package com.magina.antiPro.exception;

//import org.apache.poi.ss.formula.functions.T;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.autoconfigure.web.ErrorAttributes;
//import org.springframework.boot.autoconfigure.web.ErrorController;
//import org.springframework.boot.autoconfigure.web.ErrorProperties;
//import org.springframework.boot.autoconfigure.web.ServerProperties;
//import org.springframework.boot.context.properties.EnableConfigurationProperties;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.MediaType;
//import org.springframework.util.Assert;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.Map;
//
///**
// * @Author: gmy
// * @Description: Springboot全局异常统一处理
// * @Date: 2018/5/30
// * @Time: 16:41
// */
//@RestController
//@EnableConfigurationProperties({ServerProperties.class})
//public class ExceptionController implements ErrorController {
//
//    private ErrorAttributes errorAttributes;
//
//    @Autowired
//    private ServerProperties serverProperties;
//
//
//    /**
//     * 初始化ExceptionController
//     * @param errorAttributes
//     */
//    @Autowired
//    public ExceptionController(ErrorAttributes errorAttributes) {
//        Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
//        this.errorAttributes = errorAttributes;
//    }
//
//
//    @RequestMapping(value = "/error")
//    @ResponseBody
//    public ResponseMap<T>  error(HttpServletRequest request) {
//        Map<String, Object> body = getErrorAttributes(request,
//                isIncludeStackTrace(request, MediaType.ALL));
//        HttpStatus status = getStatus(request);
////        return new ResponseMap(status.value(),null,body.get("message").toString());
//        return new ResponseMap();
//    }
//
//
//
//    @RequestMapping(value = "/setExceptionCode")
//    @ResponseBody
//    public ResponseMap<MsgCenterException> setExceptionCode( ExceptionCode exceptionCode) {
//        ResponseMap<MsgCenterException> responseMap = new ResponseMap();
//        responseMap.setSuccess(exceptionCode.getCode() == 200 ? true : false);
//        responseMap.setMessage(exceptionCode.getMsg());
//        return new ResponseMap();
//    }
//
//
//
//
//
//    /**
//     * Determine if the stacktrace attribute should be included.
//     * @param request the source request
//     * @param produces the media type produced (or {@code MediaType.ALL})
//     * @return if the stacktrace attribute should be included
//     */
//    protected boolean isIncludeStackTrace(HttpServletRequest request,
//                                          MediaType produces) {
//        ErrorProperties.IncludeStacktrace include = this.serverProperties.getError().getIncludeStacktrace();
//        if (include == ErrorProperties.IncludeStacktrace.ALWAYS) {
//            return true;
//        }
//        if (include == ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM) {
//            return getTraceParameter(request);
//        }
//        return false;
//    }
//
//
//    /**
//     * 获取错误的信息
//     * @param request
//     * @param includeStackTrace
//     * @return
//     */
//    private Map<String, Object> getErrorAttributes(HttpServletRequest request,
//                                                   boolean includeStackTrace) {
//        RequestAttributes requestAttributes = new ServletRequestAttributes(request);
//        return this.errorAttributes.getErrorAttributes(requestAttributes,
//                includeStackTrace);
//    }
//
//    /**
//     * 是否包含trace
//     * @param request
//     * @return
//     */
//    private boolean getTraceParameter(HttpServletRequest request) {
//        String parameter = request.getParameter("trace");
//        if (parameter == null) {
//            return false;
//        }
//        return !"false".equals(parameter.toLowerCase());
//    }
//
//    /**
//     * 获取错误编码
//     * @param request
//     * @return
//     */
//    private HttpStatus getStatus(HttpServletRequest request) {
//        Integer statusCode = (Integer) request
//                .getAttribute("javax.servlet.error.status_code");
//        if (statusCode == null) {
//            return HttpStatus.INTERNAL_SERVER_ERROR;
//        }
//        try {
//            return HttpStatus.valueOf(statusCode);
//        }
//        catch (Exception ex) {
//            return HttpStatus.INTERNAL_SERVER_ERROR;
//        }
//    }
//
//    /**
//     * 实现错误路径,暂时无用
//     * @return
//     */
//    @Override
//    public String getErrorPath() {
//        return "";
//    }
//
//}
